package com.bookpavilion.cart.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bookpavilion.cart.client.GoodsClient;
import com.bookpavilion.cart.pojo.Cart;
import com.bookpavilion.cart.pojo.Goods;
import com.bookpavilion.cart.pojo.OrderItem;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CartService {

    @Autowired
    private GoodsClient client;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IdWorker idWorker;
    //获取购物车中是否有一样的商品
    public OrderItem getOrderItemInCart(Cart cart, String goodsId) {
        for (OrderItem orderItem : cart.getOrder_item()
        ) {
            if(orderItem.getGoods_id().equals(goodsId)){
                return orderItem;
            }

        }
        return null;
    }
    public Cart searchSameSellerCart(List<Cart> carts, String sellerId) {
        for (Cart cart :
                carts) {
            if (cart.getSellerId().equals(sellerId)){
                return cart;
            }
        }
        return null;
    }
    public List<Cart> add(List<Cart> list,String goodId,Integer num){
        list=(list==null)?new ArrayList<Cart>():list;
        Goods goods =  JSON.parseObject(JSON.toJSONString(client.findById(goodId).getData()),new TypeReference<Goods>(){});
        client.findById(goodId).getData();
        Cart cart = searchSameSellerCart(list, goods.getSeller_id());
        //Cookie中原来就有这个卖家的信息
        if (cart!=null){
            OrderItem orderItem = getOrderItemInCart(cart,goodId);
            //购物车中有这个商品
            if (orderItem!=null){
                orderItem.setNum(orderItem.getNum()+num);
                orderItem.setTotal_fee(orderItem.getPrice()*orderItem.getNum());
                if (orderItem.getNum()<=0){
                    cart.getOrder_item().remove(orderItem);
                }
                if (cart.getOrder_item().size()<=0){
                    list.remove(cart);
                }
            }
        }else {
            cart=createCart(goods,num);
            list.add(cart);
        }
    return list;

    }
    public OrderItem createOrderItem(Goods goods,Integer num){
        OrderItem orderItem = new OrderItem();
        orderItem.setGoods_id(goods.getId());
        orderItem.setNum(num);
        orderItem.setSeller_id(goods.getSeller_id());
        orderItem.setPic_path(goods.getSmall_pic());
        orderItem.setPrice(goods.getSale_price());
        orderItem.setTitle(goods.getGoodsname());
        orderItem.setTotal_fee(goods.getSale_price()*num);
        return orderItem;
    }
    public Cart createCart(Goods goods,Integer num){
        Cart cart =new Cart();
        cart.setSellerId(goods.getSeller_id());
        cart.setId(idWorker.nextId()+"");
        OrderItem orderItem = createOrderItem(goods, num);
        List<OrderItem> orderItems = new ArrayList<>();
        orderItems.add(orderItem);
        cart.setOrder_item(orderItems);
        return cart;
    }
    public Cart findAll(String userId){
        Cart cart = (Cart) redisTemplate.opsForHash().get("bp_cart", userId);
        return cart;
    }
    public List<Cart> convertCartFromCookieToRedis(List<Cart> redisCart,List<Cart> cookieCart){
        for (Cart cart:cookieCart
             ) {
            for (OrderItem orderItem: cart.getOrder_item()
                 ) {
                redisCart=add(redisCart,orderItem.getGoods_id(), orderItem.getNum());
            }
        }
        return redisCart;
    }
    public void saveCartsToRedis(String userId,List<Cart> carts){
        redisTemplate.opsForHash().put("bp-cart",userId,carts);
    }
    public List<Cart> findCartsFromRedis(String userId){
        List<Cart> carts = (List<Cart>) redisTemplate.opsForHash().get("bp-cart", userId);
        if (carts==null){
            carts=new ArrayList<>();
        }
        return carts;
    }


}
